/** CardCommonComputed
 * [CardCommonComputed description]
 * @type {Object}
 * 只放 Card 必要且共用的 computed / methods
 * 目前 cardLists 的数组层级为两层，即 cardLists: [ { card_group/sub_cards: [] } ]
 *             pages[curPage] => cardLists
 * card_group/sub_cards => cardGroup
 */

import { isArray, isString } from '@/utils/public.js';
import { mapState, mapActions } from 'vuex'
const CardCommonComputed = {
  computed: {
    // TODO: 分类
    ...mapState({
      pages: state => _.cloneDeep(state.cardComponents.siteData.pages),
      curPage: state => state.cardComponents.curPage,
      curPageData: state => state.cardComponents.curPageData,
      curTarget: state => _.cloneDeep(state.cardComponents.curTarget),  // 当前选择/操作的 card item
    }),
    cardLists() {
      return this.curPageData || [];
    },
    cardGroup() {  /* 获取 组合类card 的子集，非组合类card返回 null */
      if(this.curSelectParentCardIndex < 0) return null;
      const strCardType = this.cardLists[this.curSelectParentCardIndex].card_type;
      if(strCardType == 11) return this.cardLists[this.curSelectParentCardIndex].card_group;
      if(strCardType == 86) return this.cardLists[this.curSelectParentCardIndex].sub_cards;
      if(strCardType == 118) return this.cardLists[this.curSelectParentCardIndex].items;
      return null;
    },
    combCardLists() {
      return (item) => {
        const itemCard_type = 'CARD_' + item.card_type;
        if(itemCard_type === 'CARD_11') return item.card_group;
        if(itemCard_type === 'CARD_86') return item.sub_cards;
        if(itemCard_type === 'CARD_118') return item.items;
        return null;
      }
    },
    bIsParentEditable() {  /* 是否正在 编辑列表（即编辑组合类card的子集） */
      return isString(this.curTarget.parentId) && this.curTarget.parentId != '' ? true : false;
    },
    curSelectCardIndex() {  /* 获取当前选择/操作的 card 的索引，不存在则返回 -1 */
      const that = this;
      let newIndex = -1;

      // 判断是否 cardLists or cardGroup
      const lists = this.bIsParentEditable ? this.cardGroup : this.cardLists;
      isArray(lists) && lists.filter((item, curIndex) => {
        if(that.curTarget.itemId == item.itemid) {
          newIndex = curIndex;
          return item;
        }
      });

      return newIndex;
    },
    curSelectParentCardIndex() {  /* 获取当前选择/操作的 card 的父级在数组中的索引，不存在则返回 -1 */
      // if > 0 ?
      const that = this;
      let newIndex = -1;

      // 判断是否 bIsParentEditable, false 则返回 -1
      if(!this.bIsParentEditable) return newIndex;
      this.cardLists.filter((item, curIndex) => {
        if(that.curTarget.parentId == item.itemid) {
          newIndex = curIndex;
          return item;
        }
      });

      return newIndex;
    },
    curSelectCardType() {  /* 获取当前选择/操作的 card 的类型，无则返回 false */
      // 判断是否 cardLists or cardGroup
      const lists = this.bIsParentEditable ? this.cardGroup : this.cardLists;
      const curSelectCard = this.curSelectCardIndex >= 0 ? lists[this.curSelectCardIndex] : null;
      if(curSelectCard) return 'CARD_' + curSelectCard.card_type;
      return false;
    },
    bIsCardTypeOfComb() {  /* 判断是否属于 组合类card，如 card 11 / card 86 / card 118 等 */
      // 闭包实现传参，不会缓存；但为了归属于参数类，使用此方法
      return (cardType) => {
        return cardType === 'CARD_11'
          || cardType === 'CARD_86'
          || cardType === 'CARD_118';
      }
    },

    curTargetItem() {
      const newCardLists = this.bIsParentEditable ? _.cloneDeep(this.cardGroup) : _.cloneDeep(this.cardLists);
      return this.curSelectCardIndex > -1 ? newCardLists[this.curSelectCardIndex] : null;
    },
    curTargetItemParent() {
      return this.bIsParentEditable ? _.cloneDeep(this.cardLists[this.curSelectParentCardIndex]) : null;
    },
    curLists() {
      return this.bIsParentEditable ? _.cloneDeep(this.cardGroup) : _.cloneDeep(this.cardLists);
    },
  },
  methods: {
    ...mapActions({
      updateCardListsOriginal: 'cardComponents/updateCardLists',
      updateCurTarget: 'cardComponents/updateCurTarget',
    }),
    updateCardLists(arr) {
      if(this.bIsParentEditable) this.updateDrag4CardGroup(arr);
      else this.updateDrag4CardLists(arr);
    },
    updateDrag4CardLists(arr) {
      // 1. 更新 cardLists 的 Idx_index
      arr = this.setCardItemsIndex(arr);

      // 2. updateCardListsOriginal
      this.updateCardListsOriginal(arr);
    },
    updateDrag4CardGroup(arr) {
      // 1. 更新 cardLists 的 Idx_index
      arr = this.setCardItemsIndex(arr);

      // 2. updateCardListsOriginal
      let newCardLists = _.cloneDeep(this.cardLists);
      const strCardType = newCardLists[this.curSelectParentCardIndex].card_type;
      if(strCardType == 11) newCardLists[this.curSelectParentCardIndex].card_group = arr;
      if(strCardType == 86) newCardLists[this.curSelectParentCardIndex].sub_cards = arr;
      if(strCardType == 118) newCardLists[this.curSelectParentCardIndex].items = arr;
      this.updateCardListsOriginal(newCardLists);
    },
    setCardItemsIndex(lists) {  /* cardlists Idx_index 排序 */
      return lists.map((item, index) => {
        item.Idx_index = index.toString();
        return item;
      });
    },
  }
};



export default CardCommonComputed;
